//
//  NSString+Timestamp.m
//  beichoo_N_ios
//
//  Created by 卢仕彤 on 2017/9/12.
//  Copyright © 2017年 陈健. All rights reserved.
//

#import "NSString+Timestamp.h"

@implementation NSString (Timestamp)

+ (NSString *)ArticleComment:(NSString *)commentTime {
    NSTimeInterval time=[commentTime doubleValue];
    
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"MM.dd HH:mm"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

+ (NSString *)GeneralTime:(NSString *)generalTime withFormat:(NSString *)formatString {
    NSTimeInterval time=[generalTime doubleValue];

    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    //实例化一个NSDateFormatter对象
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:formatString];
    
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    return currentDateStr;
}

+ (NSString *)timeMessage:(double)beTime {
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;
    
    if (distanceTime < 60*10) {//小于一分钟
        distanceStr = @"刚刚";
    }
    else if (distanceTime <60*60 && distanceTime > 60*10) {//时间小于一个小时
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
    }
    else if(distanceTime<24*60*60  && distanceTime > 60*60 ){
        distanceStr = [NSString stringWithFormat:@"%ld小时前",(long)distanceTime/3600];
        
    }
    else if(distanceTime <24*60*60*7 && distanceTime>24*60*60){
        distanceStr = [NSString stringWithFormat:@"%ld天前",(long)distanceTime/3600/24];
    }
    else if(distanceTime <24*60*60*7*4 && distanceTime>24*60*60*7){
        distanceStr = [NSString stringWithFormat:@"%ld周前",(long)distanceTime/3600/24/7];
    }
    else if (distanceTime >24*60*60*30 && distanceTime <24*60*60*365){
        distanceStr = [NSString stringWithFormat:@"%ld月前",(long)distanceTime/3600/24/30];
    }
    else{
        distanceStr = [NSString stringWithFormat:@"%ld年前",(long)distanceTime/3600/24/30/12];
    }
    return distanceStr;
}
/*
 首先对比 年
 如果现在
 */
+ (NSString *)show_detaileOftimeMessage:(double)beTime {
    beTime = beTime;
    NSString *result;
    int min = 60000;

    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval correntInterval = [date timeIntervalSince1970]*1000;
    if (correntInterval - beTime < min * 30) {
        result = @"刚刚";
    }
    else {
        //传入时间
        NSDate *detaildate = [NSDate dateWithTimeIntervalSince1970:beTime];
        NSDate *currentDate = [NSDate dateWithTimeIntervalSinceNow:0];
        NSInteger i = [self calcDaysFromBegin:detaildate end:currentDate];
        if (i >= 0) {
            NSDateFormatter *simpleDateFormatt = [[NSDateFormatter alloc] init];
            [simpleDateFormatt setDateFormat:@"HH:mm"];
            if (i < 1) {
                result = [simpleDateFormatt stringFromDate:detaildate];
            }
            else if (i < 2) {
                result = [NSString stringWithFormat:@"昨天%@",[simpleDateFormatt stringFromDate:detaildate]];
            }
            else if (i < 3) {
                result = [NSString stringWithFormat:@"前天%@",[simpleDateFormatt stringFromDate:detaildate]];
            } else if (i < 360) {
                NSDateFormatter *monthDateFormat = [[NSDateFormatter alloc] init];
                [monthDateFormat setDateFormat:@"MM月dd日"];
                result = [monthDateFormat stringFromDate:detaildate];
            } else {
                NSDateFormatter *yearDateFormat = [[NSDateFormatter alloc] init];
                [yearDateFormat setDateFormat:@"yyyy年MM月dd日"];
                result = [yearDateFormat stringFromDate:detaildate];
            }
        }
        else {
            result = @"刚刚";
        }
    }    return result;
}

+ (NSInteger) calcDaysFromBegin:(NSDate *)beginDate end:(NSDate *)endDate {
    //创建日期格式化对象
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    
    endDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:endDate]];
    beginDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:beginDate]];
    //取两个日期对象的时间间隔：
    //这里的NSTimeInterval 并不是对象，是基本型，其实是double类型，是由c定义的:typedef double NSTimeInterval;
    NSTimeInterval time = [endDate timeIntervalSinceDate:beginDate];
    
    int days = ((int)time)/(3600*24);
    return days;
}

+ (NSString *)getTimeFromNSDate:(NSDate *)date {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy年MM月dd日"];
    NSString *dateStr = [dateFormatter stringFromDate:date];
    return dateStr;
}
@end
